MySQL查询语句(select)详解(1)

您所在的位置:网站首页 select * 和select 所有字段 MySQL查询语句(select)详解(1)

MySQL查询语句(select)详解(1)

#MySQL查询语句(select)详解(1)| 来源: 网络整理| 查看: 265

1.查询记录

select*from 表名 [where 条件];

eg:select*from students;//查询 students 表中所有记录,所有字段的值都显示出来

select field1,field2,...fieldn... from 表名 [where 条件];

eg:select id,name,age from students;//查询 students 表中所有记录, 只显示出 id,name,age三个字段的值

 

1.“*”表示将所有的字段都显示出来

2.用逗号分割,列出需要显示的字段

 

2.查询不重复的记录

select distinct 字段 from 表名;

eg: select distinct name from students;//查询名字不相同的学生;  select distinct name,age from students;//查询名字和年龄同时不同的学生    1.distinct必须放在最开头  2.distinct只能使用需要去重的字段进行操作。 ----也就是说我sidtinct了name,age两个字段,我后面想根据id进行排序,是不可以的,因为只能name,age两个字段进行操作.  3.distinct去重多个字段时,含义是:几个字段 同时重复 时才会被 过滤。

 

 3.条件查询

select 字段 from 表名 where 条件; eg:select * from student where sex='男' and age>20; //查询性别是男,并且年龄大于20岁的人。

where后面的条件可以用>、=、4;

统计薪水总额,最低薪资,最高薪资 select count(1),min(salary),max(salary) from A;

 

6.表连接

表连接分为内连接和外连接。

他们之间最主要的区别:内连接仅选出两张表中互相匹配的记录,外连接会选出其他不匹配的记录。

以下是员工表staff和职位表deptno:

内连接 select staff.name,deptname from staff,deptno where staff.name=deptno.name;

 

外连接 分为左连接和右连接

左连接:包含所有左边表中的记录,甚至是右边表中没有和他匹配的记录。

右连接:包含所有右边表中的记录,甚至是右边表中没有和他匹配的记录。

 

外连接(左连接): select staff.name,deptname from staff left join deptno on staff.name=deptno.name;

外连接(右连接): select deptname,deptno.name from staff right join deptno on deptno.name=staff.name;

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3